import { createRouter, createWebHistory } from 'vue-router'
import Dashboard from '@/views/Dashboard/index.vue'
import Layout from '@/views/Layout/index.vue'
import Login from '@/views/Login/index.vue'
import Map from '@/views/Map/index.vue'
import LoginView from '@/views/Login/components/Login.vue'
import Register from '@/views/Login/components/Register.vue'
const router = createRouter({
  history: createWebHistory('/'), // 路由模式
  routes: [
    {
      path: '/',
      component: Layout,
      children: [
        {
          path: '/Map',
          component: Map
        },
        {
          // 默认页面会得到渲染，因此这里设置为空字符串
          path: '',
          component: Dashboard
        },
        {
          path: '/Analysis',
          component: () => import('@/views/Analysis/index.vue'),
          children: [
            {
              path: '/Analysis/components/AnaMain',
              component: () => import('@/views/Analysis/components/AnaMain.vue')
            },
            {
              path: '/Analysis/components/AnaMct',
              component: () => import('@/views/Analysis/components/AnaMct.vue')
            },
            {
              path: '/Analysis/components/AnaCas',
              component: () => import('@/views/Analysis/components/AnaCas.vue')
            }
          ]
        },
        {
          path: '/Farm',
          component: () => import('@/views/Farm/index.vue')
        }
      ]
    },
    {
      path: '/login',
      component: Login,
      redirect: '/login',
      children: [
        {
          path: '',
          component: LoginView
        },
        {
          path: '/Register',
          component: Register
        }
      ]
    }
  ],
})



//路由守卫 - 检查认证状态
router.beforeEach((to, form, next) => {
  const isAuthenticated = localStorage.getItem('isAuthenticated')

  if(to.meta.requiresAuth && !isAuthenticated) {
    next('/login')
  }else if(to.meta.requiresAuth && isAuthenticated) {
    next('/')
  }else{
    next()
  }
})
export default router

